<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class HttpResponse {

	const NEWLINE = "(\r?\n)";

	private $header = '';
	public function getHeader() { return $this->header; }

	private $content = '';
	public function getContent() { return $this->content; }

	public function __construct($rawContent) {

		//var_dump($rawContent);
		$arr = split(self::NEWLINE . self::NEWLINE, $rawContent, 2);

		if (!$arr) {

			$this->header = '';
			$this->content = '';

		} else if (count($arr) == 1) {

			$this->header = $rawContent;
			$this->content = '';

		} else {
			$this->header = $arr[0];
			$this->content = $arr[1];
		}
	}

	public function getContentAsLinesArray() {
		$result = array();
		foreach (split(self::NEWLINE, $this->content) as $line) {
		 	$result[] = $line;
		}
		return $result;
	}

	public function getCookies() {
		return preg_match_all('~Set-Cookie:\s+([^;]+);~', $this->getHeader(), $m) ?
			$m[1] :
			array();
	}
}

?>
